﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CUMTConnect
{
	class WIFI
	{
		/**
		 * Get the current linked wifi SSID
		 */
		public static String GetCurrentSSID()
		{
			Process process = new Process
			{
				StartInfo =
				{
					FileName = "netsh.exe",
					Arguments = "wlan show interfaces",
					UseShellExecute = false,
					RedirectStandardOutput = true,
					CreateNoWindow = true
				}
			};
			process.Start();
			var output = process.StandardOutput.ReadToEnd();
			var line = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(l => l.Contains("SSID") && !l.Contains("BSSID"));
			if(line == null)
			{
				return string.Empty;
			}
			return line.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[1].TrimStart();
		}

		/**
		 * Scan the all the wifi and output it's SSID
		 */
		public static String ScanSSID()
		{
			Process process = new Process
			{
				StartInfo =
				{
					FileName = "netsh.exe",
					Arguments = "wlan show networks",
					UseShellExecute = false,
					RedirectStandardOutput = true,
					CreateNoWindow = true
				}
			};
			process.Start();
			String output = process.StandardOutput.ReadToEnd();
			StringBuilder stringBuilder = new StringBuilder();
			foreach (Match match in Regex.Matches(output, @"SSID.*\n"))
			{
				stringBuilder.Append(match.Value.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[1].TrimStart().TrimEnd());
				stringBuilder.Append(";");
			}
			return stringBuilder.ToString();
		}
	}
}
